﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.Service;
using Client.View;
using Client.View.CommonView;
using Client.View.UserView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.ViewModel.UserViewModel
{
    public class UserEditViewModel : BindableBase
    {
        public UserEdit Page { get; private set; }

        public UserEditViewModel(UserEdit page)
        {
            Page = page;

            OrgTreeUserControl = new OrgTree
            {
                DataContext = this
            };

            SaveCommand = new DelegateCommand(SaveInfo);

            ParentOrgGotFocusCommand = new DelegateCommand(ParentOrgGotFocus);
            SelectedOrgCommand = new DelegateCommand<ViewOrg>(SelectedOrg);

            MsgEventSubscribe();

            RoleService.Instance().SubscribeRefreshRolesEvent(RefreshAllRoles);

            OrgService.Instance().SubscribeRefreshOrgTreeEvent(RefreshOrgTree);
            if (OrgService.Instance().RootOrgTree == null)
            {
                OrgService.Instance().InitOrgTree();
            }
            else
            {
                OrgTree = OrgService.Instance().OrgTree;
            }

        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(ViewUser user, UserListViewModel parentDataContext)
        {
            ParentDataContext = parentDataContext;
            AllRoles = RoleService.Instance().RoleList;
            Title = user == null ? "新增用户" : "编辑用户";
            if (user == null)
            {
                ViewUser = new ViewUser();
                return;
            }
            ViewUser = user;
        }


        private void RefreshAllRoles(List<ViewRole> roles)
        {
            AllRoles = roles;
        }

        /// <summary>
        /// 所有角色
        /// </summary>
        private List<ViewRole> allRoles;

        public List<ViewRole> AllRoles
        {
            get { return allRoles; }
            set
            {
                allRoles = value;
                RaisePropertyChanged("AllRoles");
            }
        }


        /// <summary>
        /// 标题
        /// </summary>
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }

        /// <summary>
        /// 角色信息
        /// </summary>
        private ViewUser viewUser;
        public ViewUser ViewUser
        {
            get { return viewUser; }
            set
            {
                viewUser = value;
                RaisePropertyChanged("ViewUser");
            }
        }

        /// <summary>
        /// 父级页面的DataContext
        /// </summary>
        private UserListViewModel ParentDataContext { get; set; }

        /// <summary>
        /// 保存角色信息
        /// </summary>
        public DelegateCommand SaveCommand { get; private set; }
        public void SaveInfo()
        {
            AddEditUserReq msg = new AddEditUserReq(MessageId.ADD_EDIT_USER)
            {
                User = ViewUser
            };
            int roleId = viewUser.Role == null ? 0 : ViewUser.Role.ID;
            msg.RoleIds.Add(roleId);
            ApplicationClient.Instance().Send(msg);
        }

        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.OptType != (int)OptType.ADD_EDIT_USER)
                {
                    return;
                }
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ConfirmTip tip = new ConfirmTip()
                    {
                        Message = { Text = msg.Content }
                    };
                    DialogHost.Show(tip, "UserEditPage", CloseTip);
                }));
            }
        }


        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            Page.Hide();
            if (ParentDataContext != null)
            {
                ParentDataContext.GetUsers();
            }
        }




        /// <summary>
        /// 弹出组织选择窗口
        /// </summary>
        public DelegateCommand ParentOrgGotFocusCommand { get; set; }
        private void ParentOrgGotFocus()
        {
            DialogHost.Show(OrgTreeUserControl, "UserEditPage", CloseSelectParentPanel);
        }


        public void CloseSelectParentPanel(object sender, DialogClosingEventArgs eventArgs)
        {
            if (string.Equals(eventArgs.Parameter, "Cancel"))
            {
                // 取消逻辑

            }
            else
            {
                // 确定逻辑
                ViewUser.OrgId = SelectedParentOrg.ID;
                ViewUser.OrgName = selectedParentOrg.OrgName;
            }
        }



        private ViewOrg selectedParentOrg;

        public ViewOrg SelectedParentOrg
        {
            get { return selectedParentOrg; }
            set
            {
                selectedParentOrg = value;
                RaisePropertyChanged("SelectedParentOrg");
            }
        }


        public OrgTree OrgTreeUserControl { get; set; }

        private void RefreshOrgTree(List<ViewOrg> orgTree)
        {
            OrgTree = orgTree;
        }

        private List<ViewOrg> orgTree;

        public List<ViewOrg> OrgTree
        {
            get { return orgTree; }
            set
            {
                orgTree = value;
                RaisePropertyChanged("OrgTree");
            }
        }


        public DelegateCommand<ViewOrg> SelectedOrgCommand { get; set; }
        private void SelectedOrg(ViewOrg org)
        {
            SelectedParentOrg = org;
        }

        public Visibility WarnTip { get; set; } = Visibility.Hidden;



    }
}
